<template>
  <div>
    <el-card>
      <h5>商品列表</h5>
      <div>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="left" inline class="demo-table-expand">
                <el-form-item label="商品ID">
                  <span>{{ props.row.id }}</span>
                </el-form-item>
                <el-form-item label="商品名称">
                  <span>{{ props.row.name }}</span>
                </el-form-item>
                <el-form-item label="所属分类">
                  <span>{{ props.row.category }}</span>
                </el-form-item>
                <el-form-item label="商品价格">
                  <span>{{ props.row.price }}</span>
                </el-form-item>
                <el-form-item label="商品图片">
                  <span>
                    <img :src="props.row.imgUrl" alt="" style="width: 60px" />
                  </span>
                </el-form-item>
                <el-form-item label="创建时间">
                  <span>{{ props.row.ctime }}</span>
                </el-form-item>
                <el-form-item label="商品评价">
                  <span>{{ props.row.sellCount }}</span>
                </el-form-item>
                <el-form-item label="商品销量">
                  <span>{{ props.row.rating }}</span>
                </el-form-item>
                <el-form-item label="商品描述">
                  <span>{{ props.row.goodsDesc }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column label="商品名称" prop="name"> </el-table-column>
          <el-table-column label="所属分类" prop="category"> </el-table-column>
          <el-table-column label="商品价格" prop="price"> </el-table-column>
          <el-table-column label="商品图片">
            <template slot-scope="e">
              <img
                :src="e.row.imgUrl"
                alt=""
                style="width: 80px; height: 80px"
              />
            </template>
          </el-table-column>
          <el-table-column label="商品描述" prop="goodsDesc"> </el-table-column>
          <el-table-column width="200px" label="操作">
            <template slot-scope="e">
              <el-button type="primary" size="mini" @click="compile(e.row)"
                >编辑</el-button
              >
              <el-button type="danger" size="mini" @click="deleclick(e.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 修改商铺 -->
      <el-dialog title="修改商品" :visible.sync="dialogFormVisible">
        <p>
          商品名字
          <el-input
            v-model="name"
            autocomplete="off"
            style="width: 420px; height: 40px"
          ></el-input>
        </p>
        <p>
          商品分类
          <el-select v-model="value" clearable placeholder="请选择" size="mini">
            <el-option
              v-for="(e, i) in options[0]"
              :key="i"
              :value="e.cateName"
            >
            </el-option>
          </el-select>
        </p>
        <p>
          商品价格
          <el-input
            v-model="price"
            autocomplete="off"
            style="width: 420px; height: 40px"
          ></el-input>
        </p>
        <p style="display: flex">
          商品图片
          <el-upload
            style="margin-left: 5px"
            class="avatar-uploader"
            action="http://127.0.0.1:5000/goods/goods_img_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </p>
        <p>
          商品描述
          <el-input
            v-model="goodsDesc"
            autocomplete="off"
            style="width: 420px; height: 40px"
          ></el-input>
        </p>

        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="keepclick" :plain="true"
            >确 定</el-button
          >
        </div>
      </el-dialog>
      <!-- 页脚 -->
      <div class="page">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="pagesizes"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
// edit 修改
import { lista, del, categories, edit } from "@/apis/shop";
export default {
  data() {
    return {
      goodsDesc: "",
      imageUrl: "",
      price: "",
      value: "",
      name: "",
      imgUrls: "",
      dialogTableVisible: false,
      dialogFormVisible: false,
      formLabelWidth: "120px",
      currentPage: 1,
      total: 0,
      pagesizes: [4, 8, 12],
      pageSize: 3,
      tableData: [],
      options: [],
    };
  },
  methods: {
    // 上传商品图片
    handleAvatarSuccess(res, file) {
      console.log(res);
      this.imageUrl = URL.createObjectURL(file.raw);
      this.imgUrls = res.imgUrl;
      console.log(this.imgUrl, this.imageUrl);
      // bus.$emit("photo", this.imageUrl);
    },
    handleSizeChange(newsize) {
      this.pageSize = newsize;
      console.log(newsize);
      this.uplodea();
    },
    handleCurrentChange(newpage) {
      this.currentPage = newpage;
      console.log(newpage);
      this.uplodea();
    },
    // 渲染列表
    uplodea() {
      lista({ currentPage: this.currentPage, pageSize: this.pageSize }).then(
        (res) => {
          console.log(res.data.data);
          // this.imgUrls = "";
          // this.imgUrls = obj.imgUrl.substring

          for (let objs of res.data.data) {
            let tian = new Date(objs.ctime);
            objs.ctime =
              tian.getFullYear() +
              "-" +
              (tian.getMonth() + 1 < 10
                ? " 0" + (tian.getMonth() + 1)
                : tian.getMonth() + 1) +
              "-" +
              (tian.getDate() < 10 ? "0" + tian.getDate() : tian.getDate());
          }
          console.log(res);
          this.tableData = res.data.data;
          this.total = res.data.total;
        }
      );
    },
    // 编辑
    compile(row) {
      this.dialogFormVisible = true;
      this.name = row.name;
      this.price = row.price;
      this.value = row.category;
      this.goodsDesc = row.goodsDesc;
      this.id = row.id;
      this.imageUrl = row.imgUrl;
      console.log(row);
      this.imgUrls = row.imgUrl.substring(row.imgUrl.lastIndexOf("/") + 1);
    },
    // 保存修改
    keepclick() {
      edit({
        name: this.name,
        category: this.value,
        price: this.price,
        imgUrl: this.imgUrls,
        goodsDesc: this.goodsDesc,
        id: this.id,
      }).then((res) => {
        if (res.data.code == 0) {
          this.dialogFormVisible = false;
          this.uplodea();

          this.$message({
            message: "修改成功",
            type: "success",
          });
        }
      });
    },
    // 删除
    deleclick(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          del({ id: row.id }).then((res) => {
            if (res.data.code == 0) {
              this.uplodea();
            }
          });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  created() {
    this.uplodea();

    categories().then((res) => {
      // console.log(res.data);

      for (const key in res.data) {
        this.options.push(res.data[key]);
      }
    });
  },
};
</script>

<style lang="less" scoped>
h5 {
  line-height: 30px;
  font-weight: normal;
  margin-bottom: 15px;
  border-bottom: 1px solid #ebeef5;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.page {
  margin: 20px;
}
p {
  margin-bottom: 15px;
}
.avatar-uploader,
.el-upload {
  width: 80px;
  height: 80px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 80px;
  height: 80px;
  line-height: 80px;
  text-align: center;
}
.avatar {
  width: 80px;
  height: 80px;
  display: block;
}
</style>